import logManager from '../service/LogService'

import en from '@locales/en.json'
import zh from '@locales/zh.json'

type MessageSchema = typeof zh
const messages: Record<string, MessageSchema> = { en, zh }

export function createTranslator() {
  return (key?: string) => {
    if (!key) return void 0
    try {
      const keys = key?.split('.')
      let result: any = messages['zh']
      for (const _key of keys) {
        result = result[_key]
      }
      return result as string
    } catch (e) {
      logManager.error('failed to translate key:', key, e)
      return key
    }
  }
}
